// pages/goods_detail/index.js
import {request} from '../../utils/request.js'
Page({

    /**
     * 页面的初始数据
     */
    data: {
        id:null,
        goods:null,
        collectActive:false
    },

    urls:[],
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        let {id} = options;
        this.setData({id})
        this.loadGoodsDetail();


       
    },

    loadGoodsDetail:function(){
        let id = this.data.id;
        request({   url:'/goods/detail',  data:{ goods_id:id  } }).
        then((goods)=>{
            this.setData({goods}) ;
            this.urls = goods.pics.map(v=>v.pics_big_url);

            let collect = wx.getStorageSync('collect')||[];
            console.log(collect);
            collect.forEach(e=>{
               if( e.id == goods.goods_id){
                    this.setData({collectActive:true})
                   return ;
               }
            })

        })
    },

    //预览大图
    handleBigPic:function(){
       
        wx.previewImage({
            urls:this.urls
        })
    },



      /**
   * 用户点击右上角分享
   */
    onShareAppMessage: function () {
        return {
            "title":'产品分享',
            "path":'/pages/goods_detail/goods_detail?id='+this.data.id,
            "imageUrl":this.data.goods.goods_small_logo
        }
    },
    handleCollect:function(){
        let goods = this.data.goods;
        let product ={
            id:goods.goods_id,
            name:goods.goods_name,
            price:goods.goods_price,
            sma_pic:goods.goods_small_logo,
        }

        let exists = false;
        
        let collect = wx.getStorageSync('collect')||[];
        collect.forEach(e=>{
           if( e.id == goods.goods_id){
               exists = true;
               return ;
           }
        })
        if(exists){
            return;
        }
        collect.push(product);
        wx.setStorageSync('collect', collect);
        this.setData({collectActive:true});
    },

    handleAddCart:function(){
        let goods = this.data.goods;
        let product ={
            id:goods.goods_id,
            name:goods.goods_name,
            price:goods.goods_price,
            num:1,
            sma_pic:goods.goods_small_logo,
        }

        let exists = false;
        
        let cart = wx.getStorageSync('cart')||[];
        cart.forEach(e=>{
           if( e.id == goods.goods_id){
               exists = true;
               return ;
           }
        })
        if(exists){
            wx.showToast({
                title: '已添加',
                icon: 'success',
                duration: 1500,
                mask: true,
               
            });
            return;
        }
        cart.push(product);
        wx.setStorageSync('cart', cart);
        wx.showToast({
            title: '添加成功',
            icon: 'success',
            duration: 1500,
            mask: true,
            
        });
    },
    toCart:function(){
        wx.switchTab({
            url: '/pages/cart/index',
        });
          
    }


})

